<template>
	<view>
		<city-select :activeCity="activeCity" :locationCity="locationCity" :obtainCitys="obtainCitys"
			:formatName="formatName" :hotCity="hotCity" ref="city" @cityClick="selectCity"></city-select>
	</view>
</template>

<script>
	import citySelect from "@/components/city-select/city-select.vue"
	import WxMap from 'common/js/map-wx-jssdk.js'

	export default {
		components: {
			citySelect
		},
		data() {
			return {
				activeCity: null,
				locationCity: null,
				obtainCitys: [],
				hotCity: [],
				formatName: 'title',
				cityAddon: 0
			}
		},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();
			this.getCityList();
			this.getLocation();
			this.checkAddonIsexit();
			if (uni.getStorageSync('city')) this.activeCity = uni.getStorageSync('city');
		},
		methods: {
			getCityList() {
				if (uni.getStorageSync('cityData')) this.obtainCitys = uni.getStorageSync('cityData');
				this.$api.sendRequest({
					url: '/api/address/city',
					success: res => {
						if (res.data) {
							this.obtainCitys = res.data;
							this.$refs.city.updateCitys(res.data);
							uni.setStorageSync('cityData', res.data);
						}
					}
				})
			},
			selectCity(data) {
				this.$api.sendRequest({
					url: '/api/website/getWebsiteidByAddress',
					data: {
						web_city: data.id
					},
					success: res => {
						if (res.code >= 0) {
							if (res.data) {
								uni.setStorage({
									key: 'city',
									data: data,
									success: () => {
										this.$util.goBack();
									}
								})
								uni.setStorage({
									key: 'website_id',
									data: res.data.site_id,
									success: () => {

									}
								})
								uni.setStorage({
									key: 'websiteInfoStatus',
									data: res.data.status,
									success: () => {

									}
								})
							} else {
								// this.$util.showToast({
								// 	title: '该城市暂无站点，即将切换成全国'
								// });

								setTimeout(() => {
									uni.setStorage({
										key: 'city',
										data: data,
										// data: {id: 0, title: '全国'},
										success: () => {
											this.$util.goBack();
										}
									})
									uni.setStorage({
										key: 'website_id',
										data: 0,
										success: () => {

										}
									})
								}, 1500)
							}
						}
					}
				})
			},
			/**
			 * 获取定位城市
			 */
			getLocation() {
				const map = new WxMap({
					key: this.$config.mpKey
				});
				uni.getLocation({
					type: 'gcj02',
					success: (res) => {
						console.log('获取到位置信息', res)
						this.$api.sendRequest({
							url: '/api/member/setUserWebsiteInfo',
							data: {
								user_latitude: res.latitude,
								user_longitude: res.longitude
							},
							success: res => {
								if (res.code == 0) {
									this.locationCity = res.data;
								}
							}
						})
						// map.reverseGeocoder({
						// 	location: {
						// 		latitude: res.latitude,
						// 		longitude: res.longitude
						// 	},
						// 	success: res => {
						// 		this.$api.sendRequest({
						// 			url: '/api/address/citybyname',
						// 			data: {
						// 				city: res.result.address_component.city
						// 			},
						// 			success: res => {
						// 				if (res.data) {
						// 					this.locationCity = res.data;
						// 				}
						// 			}
						// 		})
						// 	},
						// 	fail: res => {
						// 		console.error('定位失败错误信息：' + res.message)
						// 	}
						// })
					}
				})
			},
			checkAddonIsexit() {
				this.$api.sendRequest({
					url: '/api/addon/isexit',
					data: {
						name: 'city'
					},
					success: res => {
						this.cityAddon = res.data;
					}
				})
			}
		}
	}
</script>